import { defineStore } from 'pinia';
import { ref } from 'vue';
import { themeConfig } from '@/config/theme';
import { ls } from '@/utils/ls.utils';

export const useThemeStore = defineStore('theme', () => {
  if (!themeConfig.themeOptions) {
    console.error('themeConfig.themeOptions is not defined');
  }

  // 主题选项
  const themeOptions = ref(themeConfig.themeOptions);

  const _theme = ls.get('theme', themeOptions.value[0].value);
  // 判断 _theme 是否在 themeOptions 中
  const theme = ref(themeOptions.value.find(item => item.value === _theme) ? _theme : themeOptions.value[0].value);

  // 更新主题
  const setTheme = (themeName: unknown) => {
    if (!themeConfig.themeOptions.find(item => item.value === themeName)) {
      console.error('themeName is not in themeOptions');
      return;
    }
    theme.value = themeName as string;
    ls.set('theme', themeName);
  };

  // 获取主题
  const getTheme = () => {
    return themeConfig.themeData[theme.value as keyof typeof themeConfig];
  };

  return {
    theme,
    themeOptions,
    setTheme,
    getTheme
  };
});
